package com.sjgtw.cloud.web.duty.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sjgtw.cloud.common.core.web.controller.BaseController;
import com.sjgtw.cloud.common.core.web.domain.AjaxResult;
import com.sjgtw.cloud.common.core.web.page.TableDataInfo;
import com.sjgtw.cloud.util.EasyExcelUtil;
import com.sjgtw.cloud.web.commandoUnits.domain.CommandoUnits;
import com.sjgtw.cloud.web.commandoUnits.domain.vo.CommandoUnitsImportVo;
import com.sjgtw.cloud.web.duty.domain.Duty;
import com.sjgtw.cloud.web.duty.service.IDutyService;
import com.sjgtw.cloud.web.duty.vo.DutyImportVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 值班表Controller
 * 
 * @author ruoyi
 * @date 2024-03-13
 */
@RestController
@Api(tags = "1.3.1、值班管理/防洪值班")
@RequestMapping("/duty")
public class DutyController extends BaseController
{
    @Autowired
    private IDutyService dutyService;

    /**
     * 查询值班表列表
     */
    @GetMapping("/list")
    @ApiOperation("列表")
    public TableDataInfo list(Duty duty)
    {
        startPage();
        List<Map<String, Object>> list = dutyService.selectDutyList(duty);
        return getDataTable(list);
    }

    /**
     * 新增值班表
     */

    @PostMapping("/addOrEdit")
    @ApiOperation("新增或者编辑")
    public AjaxResult addOrEdit(@RequestBody JSONObject json)
    {
        return toAjax(dutyService.addOrEdit(json));
    }

    @DeleteMapping(value = "/delete/{id}")
    @ApiOperation("删除")
    public AjaxResult delete(@PathVariable("id") Long id)
    {
        return AjaxResult.success(dutyService.deleteDuty(id));
    }


    /**
     * 查询值班表列表
     */
    @GetMapping("/getUserDuty")
    @ApiOperation("返回某个人的值班日期")
    public AjaxResult getUserDuty(Long workerId)
    {
        List<Map<String,Object>> list = dutyService.getUserDuty(workerId);
        return AjaxResult.success("200",list);
    }


    /**
     * 导出
     */
    @ApiOperation(value = "导出")
    @PostMapping("/export")
    public void export(HttpServletResponse response, DutyImportVo dutyImportVo)
    {
        List<DutyImportVo> list = dutyService.selectDutyExportList(dutyImportVo);

        EasyExcelUtil<DutyImportVo> util = new EasyExcelUtil<>(DutyImportVo.class);
        util.exportExcel(response, list, "防洪值班", "防洪值班", "duty.xlsx");
    }

    /**
     * 下载模版
     * @param response
     */
    @ApiOperation(value = "下载模版")
    @PostMapping("/downloadTemplate")
    public void downloadTemplate(HttpServletResponse response)
    {
        EasyExcelUtil<DutyImportVo> util = new EasyExcelUtil<>(DutyImportVo.class);
        util.exportExcel(response, new ArrayList<>(), "防洪值班", "防洪值班模版", "duty.xlsx");
    }


    /**
     * 导入
     * @param file
     * @return
     */
    @ApiOperation(value = "导入")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file)
    {
        EasyExcelUtil<DutyImportVo> util = new EasyExcelUtil<>(DutyImportVo.class);
        List<DutyImportVo> entities = util.readExcel(file, 0, 1);
        if (entities == null || entities.size() == 0) {
            return error("没有读取到数据");
        }

        List<String> errors = dutyService.check(entities);
        if (errors.size() > 0) {
            return AjaxResult.error("", errors);
        }

        dutyService.batchSave(entities);
        return success();
    }





}
